package com.kuangstudy.service.archives;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kuangstudy.entity.Blog;
import com.kuangstudy.mapper.BlogMapper;
import com.kuangstudy.vo.ArticleYearVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangfushuai
 * @description
 * @since 2022/4/18 11:50
 **/
@Service
@Slf4j
public class ArchivesServiceImpl implements IArchivesService {

//    @Autowired
    @Resource
    private BlogMapper blogMapper;

    @Override
    public List<ArticleYearVo> queryBlogsVo() {
        // 1: 查询年份的分组信息:blogYearGroup=[2019,2020,2021,2022]
        List<Integer> blogYearGroup = blogMapper.findBlogYearGroup();
        // 2: 组装数据
        List<ArticleYearVo> articleYearVos = blogYearGroup.stream().map(year -> {
            ArticleYearVo articleYearVo = new ArticleYearVo();
            // 2.1： 年份放进去
            articleYearVo.setYear(year);
            // 2.2: 根据年份查询分页对应的文章
            IPage<Blog> blogPage = this.findBlogByYearMain(year, 1, 2);
            if (!CollectionUtils.isEmpty(blogPage.getRecords())) {
                // 飞哥：为什么不直接放blog，而还要搞map去放，----按需加载，因为blog很多东西我是不需要返回出去的。
                List<Map<String, Object>> mapList = blogPage.getRecords().stream().map(blog -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", blog.getId());
                    map.put("title", blog.getTitle());
                    map.put("createTime", blog.getCreateTime());
                    return map;
                }).collect(Collectors.toList());
                articleYearVo.setBlogList(mapList);
            } else {
                articleYearVo.setBlogList(new ArrayList<>());// 注意：这里为什么要放进去，原因补齐数据格式 []
            }
            // 2.3: 把查询分页的信息放入到pageMap中
            Map<String, Object> pageMap = new HashMap<>();
            pageMap.put("total", blogPage.getTotal());
            pageMap.put("pages", blogPage.getPages());
            pageMap.put("pageNo", blogPage.getCurrent());
            pageMap.put("pageSize", blogPage.getSize());
            articleYearVo.setPageMap(pageMap);
            // 2.4: 补齐数据返回vo
            return articleYearVo;
        }).sorted((a, b) -> b.getYear() - a.getYear()).collect(Collectors.toList());
        return articleYearVos;
    }

    /**
     * @author zhangfushuai
     * @description 首次访问页面的时候，加载分页的文章归档信息
     * @since 2022/4/18 17:56
     * 参数 year
     * 参数 pageNo
     * 参数 pageSize
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.kuangstudy.entity.Blog>
     **/
    public IPage<Blog> findBlogByYearMain(Integer year, Integer pageNo, Integer pageSize) {
        //1 : 设置分页
        Page<Blog> page = new Page<>(pageNo, pageSize);
        //2：设置查询条件
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id,title,create_time");
        queryWrapper.eq("DATE_FORMAT(create_time,'%Y')", year);
        queryWrapper.eq("status", 1);
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("create_time");
        // 3:查询分页
        Page<Blog> blogPage = blogMapper.selectPage(page, queryWrapper);
        // 4：返回分页
        return blogPage;
    }

    /**
     * @author zhangfushuai
     * @description 点击加载更多的时候，查询分页信息，为什么不调用findBlogByYearMain呢？因为findBlogByYearMain方法中没有封装map
     *              会返回很多的无效数据。所以这个方法其实就是将数据封装一下，返回有效数据
     * @since 2022/4/18 17:56
     * 参数 year
     * 参数 pageNo
     * 参数 pageSize
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    @Override
    public List<Map<String, Object>> findBlogByYear(Integer year, Integer pageNo, Integer pageSize) {
        // 1: 分页查询对应文章信息
        List<Blog> blogRecords = this.findBlogByYearMain(year, pageNo, pageSize).getRecords();
        // 2: 如果是null直接返回
        if (CollectionUtils.isEmpty(blogRecords)) {
            return new ArrayList<>();
        }
        // 3: 如果查询到，组装成map，按需加载返回
        return blogRecords.stream().map(blog -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", blog.getId());
            map.put("title", blog.getTitle());
            map.put("createTime", blog.getCreateTime());
            return map;
        }).collect(Collectors.toList());
    }
}
